<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

$requestedShow = getPostValue( 'requested_show' );
$offeredShow = getPostValue( 'offered_show' );

$trade_id = getPostValue( 'trade_id' );
$result = mysql_query( "SELECT gold_want, iron_want, coal_want, food_want, private_id, faction_id, accepted " .
		"FROM trade " .
		"WHERE trade_id = " . $trade_id ) or die ( "Query failed: " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );

$gold_want = $row[ 'gold_want' ];
$iron_want = $row[ 'iron_want' ];
$coal_want = $row[ 'coal_want' ];
$food_want = $row[ 'food_want' ];
$stop = false;

if( isset( $_POST[ 'faction_trade' ] ) ) {
	$faction_trade = true;
	$result2 = mysql_query( "SELECT faction_id FROM faction WHERE leader = " . $_SESSION[ 'ruler_id' ] ) or die ( "Query failed: " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$tmp_faction_id = $row2[ 'faction_id' ];
}
else {
	$faction_trade = false;
	$result2 = mysql_query( "SELECT faction_id FROM faction_member WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] ) or die ( "Query failed: " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$tmp_faction_id = $row2[ 'faction_id' ];
}

if( $row[ 'private_id' ] != -1 && $row[ 'private_id' ] != $_SESSION[ 'ruler_id' ] ) {
	$stop = true;
	$status = translate( "You can't accept this trade offer." );
}
else if( $row[ 'accepted' ] == 1 ) {
	$stop = true;
	$status = translate( "This trade offer was already accepted by someone else." );
}
else if( $row[ 'faction_id' ] != -1 ) {
	if( !$faction_trade ) {
		if( $row[ 'faction_id' ] != $tmp_faction_id ) {
			$stop = true;
			$status = translate( "This trade is only for faction members." );
		}
	}
	else {
		if( $row[ 'faction_id' ] != $tmp_faction_id ) {
			$stop = true;
			$status = translate( "You are not the leader of this faction!" );
		}
	}
}

if( !$faction_trade ) {
	$result = mysql_query( "SELECT * FROM ruler WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] ) or die ( "Query failed: " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
}
else {
	$result = mysql_query( "SELECT * FROM faction WHERE leader = " . $_SESSION[ 'ruler_id' ] ) or die ( "Query failed: " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
}
if( $row[ 'gold' ] < $gold_want ) {
	$stop = true;
	$status = translate( "You don't have enough gold." );
}
if( $row[ 'iron' ] < $iron_want ) {
	$stop = true;
	$status = translate( "You don't have enough iron." );
}
if( $row[ 'coal' ] < $coal_want ) {
	$stop = true;
	$status = translate( "You don't have enough coal." );
}
if( $row[ 'food' ] < $food_want ) {
	$stop = true;
	$status = translate( "You don't have enough food." );
}

if( $stop ) {
	if( isset( $status ) ) {
		$_SESSION[ 'accept_trade_status' ] = $status;
	}
	header( "Location: trade.php?offered=$offeredShow&requested=$requestedShow" );
}
else {
	if( !$faction_trade ) {
		mysql_query( "UPDATE ruler " .
				"SET gold = " . ( $row[ 'gold' ] - $gold_want ) . ", " .
						"iron = " . ( $row[ 'iron' ] - $iron_want ) . ", " .
						"coal = " . ( $row[ 'coal' ] - $coal_want ) . ", " .
						"food = food - " . $food_want . " " .
				"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] ) or die ( "Query failed: " . mysql_error() );
		mysql_query( "UPDATE trade " .
				"SET accepted = 1, accept_ruler = " . $_SESSION[ 'ruler_id' ] . " " .
				"WHERE trade_id = " . $trade_id ) or die ( "Query failed: " . mysql_error() );
	}
	else {
		mysql_query( "UPDATE faction " .
				"SET gold = " . ( $row[ 'gold' ] - $gold_want ) . ", " .
						"iron = " . ( $row[ 'iron' ] - $iron_want ) . ", " .
						"coal = " . ( $row[ 'coal' ] - $coal_want ) . ", " .
						"food = food - " . $food_want . " " .
				"WHERE leader = " . $_SESSION[ 'ruler_id' ] ) or die ( "Query failed: " . mysql_error() );
		mysql_query( "UPDATE trade " .
				"SET accepted = 1, accept_faction = " . $tmp_faction_id . " " .
				"WHERE trade_id = " . $trade_id ) or die ( "Query failed: " . mysql_error() );
	}

	$_SESSION[ 'accept_trade_status' ] = translate( "Trade offer accepted, the trade will be finalised at the end of the day." );
	header( "Location: trade.php?offered=$offeredShow&requested=$requestedShow" );
}
?>
